package com.zjxmyq.microserviceyida.domain;

import com.zjxmyq.microserviceyida.YidaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Data
@Component
@Slf4j
public class AccessTokenManager {
    private String accessToken;
    private long expiresAt;


    public void setExpiresAt(long expiresIn){
        this.expiresAt = System.currentTimeMillis() + ((expiresIn-1000) * 1000L); // 计算过期时间戳
    }

    public boolean isAccessTokenExpired() {
        log.info("current t = {}, expiresAt = {}", System.currentTimeMillis(),expiresAt);
        return System.currentTimeMillis() >= expiresAt;
    }

    // 实现accessToken的刷新逻辑
    public void refreshAccessToken() {
        // 实现刷新逻辑，获取新的accessToken
        AccessTokenManager accessTokenManager = YidaUtils.refreshAccessToken();
        // 更新accessToken和过期时间
        this.setAccessToken(accessTokenManager.getAccessToken());
        this.setExpiresAt(accessTokenManager.getExpiresAt());
        // 注意处理并发问题
    }
}
